package com.loan.service;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.loan.model.User;

public class UserService extends BaseService<User>{
	
	/**
	 * 获取个人信息
	 * @throws Exception 
	 */
	public User getInfo(int userId) throws Exception {
		User u = User.dao.findFirst("select * from user where id=?",userId);
		return u;
	}
 

	/**
	 * 根据手机号查找
	 */
	public User findByMobile(int cid, String mobile) {
		return User.dao.findFirst("select * from user where cid=? and mobile=?",cid,mobile);
	}

	/**
	 * 根据编码查找
	 */
	public User findByCode(String code) {
		return User.dao.findFirst("select * from user where code=?",code);
	}

	/**
	 * 根据身份证查找
	 */
	public User findByCertNum(String certNum) {
		return User.dao.findFirst("select * from user where cert_num=?",certNum);
	}

	/**
	 * 登陆 
	 */
	public User checkLogin(int cid, String mobile, String password) {
		return User.dao.findFirst("select * from user where cid=? and mobile=? and password=?",cid,mobile,password);
	}

	/**
	 * 手机号是否存在
	 */
	public boolean isExistMobile(int cid, String mobile) {
		return User.dao.findFirst("select id from user where cid=? and mobile=?",cid,mobile)!=null; 
	}

	/**
	 * 身份证是否存在
	 */
	public boolean isExistCertNum(String certNum) {
		return User.dao.findFirst("select id from user where cert_num=?",certNum)!=null; 
	}
	
	/**
	 * 用户编码是否存在
	 */
	public boolean isExistCode(String code) {
		return User.dao.findFirst("select id from user where code=?",code)!=null; 
	}

	/**
	 * 分页信息
	 * @param map 
	 * @return 
	 */
	public Page<User> list(int page, int size, Map<String, String> map) {
		return User.dao.paginate(page, size, buildSql("user",Arrays.asList("is_loan", "checks", "is_staff", "status", "cid", "info_verify_status", "card_verify_status", "service_verify_status", "cert_verify_status", "create_time", "real_name", "mobile"),map));
	}

	/**
	 * 更改手机号后，刷新新手机号
	 * @param mobile
	 * @param mobile2
	 */
	public void refreshMobile(int cid, String mobile, String mobile2) {
		List<String> tables = Arrays.asList(new String[] {"repay","loan"});
		for(String t : tables) {
			System.out.println(cid+mobile2+mobile);
			System.out.println("update "+t+" set mobile=? where cid=? and mobile=?");
			Db.update("update "+t+" set mobile=? where cid=? and mobile=?",mobile2,cid,mobile);
		}
	}
}
